package cate.common.table.d;

import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@NoteClass("战斗位置")
public class GDPos {

	//没有位置
	public static final byte P_NULL = 0;

	//具体位置枚举
	public static final byte P1 = 1;
	public static final byte P2 = 2;
	public static final byte P3 = 3;
	public static final byte P4 = 4;
	public static final byte P5 = 5;
	public static final byte P6 = 6;


	//所有位置
	public static final byte[] ALL_POS = new byte[]{P1,P2,P3,P4,P5,P6};

	//排枚举
	public static final byte R1 = 1;
	public static final byte R2 = 2;


	//所有排
	public static final byte[] ALL_ROW = new byte[]{R1,R2};

	//列枚举
	public static final byte C1 = 1;
	public static final byte C2 = 2;
	public static final byte C3 = 3;



	//所有列
	public static final byte[] ALL_COL = new byte[]{C1,C2,C3};


	//排中单位
	public static final byte[] R1_ARRAY =new byte[]{P1,P2,P3};
	public static final byte[] R2_ARRAY =new byte[]{P4,P5,P6};


	//列中单位
	public static final byte[] C1_ARRAY = new byte[]{P2,P5};
	public static final byte[] C2_ARRAY = new byte[]{P1,P4};
	public static final byte[] C3_ARRAY = new byte[]{P3,P6};


	public static boolean valid(byte pos) {
		for (byte all : ALL_POS) {
			if (pos == all) {
				return true;
			}
		}
		return false;
	}



	@NoteClass("目标相对位置")
	public static class RelativeType {

		@NoteField("同一排")
		public static final byte SAME_ROW = 1;

		@NoteField("同一列")
		public static final byte SAME_COL = 2;

		@NoteField("相邻")
		public static final byte NEAR = 3;
	}

	@NoteClass("方向")
	public static class Direction{

		@NoteField("向前排")
		public static final byte FORWARD = 1;

		@NoteField("向后排")
		public static final byte BACK = 2;
	}



	/**
	 * 相对位置检测
	 * @param type
	 * @param p1
	 * @param p2
	 * @return
	 */
	public static boolean relativeCheck(byte type, byte p1, byte p2) {
		switch (type) {
			case RelativeType.SAME_ROW:
				return findRowByPos(p1) == findRowByPos(p2);
			case RelativeType.SAME_COL:
				return findColByPos(p1) == findColByPos(p2);
			case RelativeType.NEAR:
				for (byte nearPo : findNearPos(p1)) {
					if (nearPo == p2) {
						return true;
					}
				}
				return false;
			default:
				break;
		}
		return false;
	}

	public static byte[] findPosInRow(byte row){
		switch (row){
			case R1:return R1_ARRAY;
			case R2:return R2_ARRAY;
			default:return new byte[0];
		}
	}

	public static byte findRowByPos(int pos) {
		for (byte row : ALL_ROW) {
			for (byte b : findPosInRow(row)) {
				if (b == pos) {
					return row;
				}
			}
		}
		return -1;
	}


	public static byte[] findPosInCol(byte col){
		switch (col){
			case C1:return C1_ARRAY;
			case C2:return C2_ARRAY;
			case C3:return C3_ARRAY;
			default:return new byte[0];
		}
	}

	public static byte findColByPos(int pos) {
		for (byte row : ALL_COL) {
			for (byte b : findPosInCol(row)) {
				if (b == pos) {
					return row;
				}
			}
		}
		return -1;
	}

	//查找临近位置
	public static byte[] findNearPos(byte pos){
		switch (pos){
			case P1:return new byte[]{P2,P3,P4};
			case P2:return new byte[]{P1,P5};
			case P3:return new byte[]{P1,P6};
			case P4:return new byte[]{P1,P5,P6};
			case P5:return new byte[]{P2,P4};
			case P6:return new byte[]{P3,P4};
			default:return new byte[]{};
		}
	}

	public static byte randomInSameRow(List<Byte> emptys, byte row) {
		if (emptys.isEmpty()) {
			return -1;
		}
		List<Byte> avaibles = new ArrayList<>();
		for (byte b : GDPos.findPosInRow(row)) {
			if (emptys.contains(b)) {
				avaibles.add(b);
			}
		}
		Collections.shuffle(avaibles);
		return avaibles.get(0);
	}

}
